﻿using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace blackhouse.WebNemo.Web {
    internal class RecordIdentyficator {

        #region Fields

        List<string> regClms = new List<string>();
        private string identyficatorStringFormat = String.Empty;
        private GetDisplayedIdentyficator identyficatorRef;
        private string displayFormat;

        #endregion

        #region Constructors

        public RecordIdentyficator() {
            this.displayFormat = HttpContext.Current.Request.QueryString["df"];
            if (!String.IsNullOrEmpty(displayFormat)) {
                this.displayFormat = this.displayFormat.ReplaceEx("[[", "<").ReplaceEx("]]", ">");
                this.SetRegxForUserIdentyficator(HttpUtility.UrlDecode(displayFormat));
                this.identyficatorRef = RewriteIdentyficationByUser;
            }
            else
                this.identyficatorRef = r => r.Identyficator;
        }

        #endregion

        #region Methods

        public string GetDisplayIdentyficator(GroupRecord record) {
            return this.identyficatorRef(record);
        }

        private void SetRegxForUserIdentyficator(string userSetDisplay) {
            Regex r = new Regex(@"\{(\w*):*\w*\}");
            this.identyficatorStringFormat = r.Replace(userSetDisplay, new MatchEvaluator(SetRegxEval));
        }

        private string SetRegxEval(Match m) {
            string cName = m.Groups[1].Value;
            regClms.Add(cName);
            return m.Value.ReplaceEx(cName, (regClms.Count - 1).ToString());
        }

        private string RewriteIdentyficationByUser(IGroupRecord record) {
            object[] gClms = new object[regClms.Count];
            for (int xc = 0; xc < regClms.Count; xc++) {
                switch (regClms[xc]) {
                    case "ident":
                        gClms[xc] = record.Identyficator;
                        break;
                    case "idx":
                        gClms[xc] = record.RecordId;
                        break;
                    case "ctime":
                        gClms[xc] = record.CreatedTime;
                        break;
                    case "mtime":
                        gClms[xc] = record.LastModifiedTime;
                        break;
                    default:
                        gClms[xc] = record.GetValue(regClms[xc]);
                        break;
                }
            }
            return string.Format(this.identyficatorStringFormat, gClms);
        }

        public string ApplyToUrl(string editUrl) {
            if (!String.IsNullOrEmpty(this.displayFormat))
                editUrl = editUrl + "&df=" + this.displayFormat;
            return editUrl;
        }

        #endregion

    }

    internal delegate string GetDisplayedIdentyficator(IGroupRecord record);

}